package org.september.pisces.user.permission.service;

import java.util.Date;

import org.september.pisces.user.permission.entity.Department;
import org.september.pisces.user.permission.entity.SystemUser;
import org.september.smartdao.CommonDao;
import org.september.smartdao.model.ParamMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DeptService {

	@Autowired
	private CommonDao commonDao;
	
	public Department getDepartmentByName(String name) {
		Department vo = new Department();
		vo.setName(name);
		vo.setDeleteFlag(0);
		return commonDao.getByExample(vo);
	}
	
	public void initTopDept() {
		Department vo = new Department();
		vo.setParentId(-1L);
		vo.setDeleteFlag(0);
		Department po = commonDao.getByExample(vo);
		if(po==null) {
			vo.setCode("000000");
			vo.setName("顶层部门");
			vo.setAddtime(new Date());
			vo.setDescb("系统初始化");
			vo.setFullParentId("");
			commonDao.save(vo);
		}
	}
	
	/**
	 * 直属和下级用户数量
	 * @return
	 */
	public int countAllUser(String fullDeptId) {
		ParamMap pm = new ParamMap();
		pm.put("fullDeptId", fullDeptId);
		return commonDao.count("Department.countAllUser", pm);
	}
	
	/**
	 * 直属用户数量
	 * @return
	 */
	public int countDirectUser(Long deptId) {
		SystemUser su = new SystemUser();
		su.setDeleteFlag(0);
		su.setDeptId(deptId);
		return commonDao.countByExample(su);
	}
}
